home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / utils / graphic / viewers / general / msdos / ncsa / src / pcshow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-22  |  65.6 KB  |  1,890 lines

  1. /*
  2. *
  3. *  Raster image display program for the IBM PC  
  4. *
  5. *    This program takes HDF file images or binary images, one byte per pixel,
  6. *  and displays them on the PC's color graphics screen.  The dimensions of the
  7. *  image are specified on the command line for the binary images, or are in the
  8. *  HDF file.  Each image is read from the file, then displayed in the specified
  9. *  position on the screen.  Portions of the image that do not fit on the screen
  10. *  are clipped.
  11. *
  12. *    Command line options are as follows:
  13. *        -b xdim ydim        : The dimensions of the binary file, and specifies 
  14. *                                binary file type of data
  15. *        -m mapfile            : The name of a palette for a binary file
  16. *        -v                     : Sets VGA display mode
  17. *        -e                    : Sets EGA display mode (default)
  18. *        -9                    : Sets Number 9 display mode
  19. *        -h                    : Specifies reading from an HDF file (default)
  20. *        -c                    : Center the image on the screen
  21. *        -p xwhere ywhere     : Specify an x & y position for the upper left hand
  22. *                                corner of the screen
  23. *        -a                    : Set animation mode
  24. *        -A                    : Set animation mode and specify animation of multiple
  25. *                                images from only one hdf file
  26. *        -s                    : Specifies only one binary image and name is filename
  27. *        filename            : The file containing a list of binary images or
  28. *                                an HDF file containing images
  29. *  example use:    display -b 256 256 -c -m palette.pal list.dat
  30. *     the file 'list.dat' must contain a list of file names of the images
  31. *     that are to be displayed in sequence.
  32. *
  33. *  Note:  for binary file usage, both the -b xdim ydim and the -m mapfile
  34. *        options must be used for the correct displaying of the image.
  35. *        The file name at the end of the command line is a mandatory command
  36. *        line parameter, the rest of the command line parameters are optional.
  37. *
  38. *  The options available while an image is being displayed can be shown by
  39. *        typing '?'
  40. *
  41. *  The rest of the external C functions can be found in "options.c"
  42. *
  43. *  National Center for Supercomputing Applications, University of Illinois
  44. *  153 Computing Applications Building
  45. *  605 E. Springfield Ave.
  46. *  Champaign, IL  61820     (217)244-0072
  47. *
  48. *  Quincey Koziol            August 1988
  49. *
  50. */
  51.  
  52. #include "show.h"        /* include file for other include files and variables */
  53.  
  54. /* declarations of types for routines used */
  55.  
  56. long DFgetelement();    /* HDF call to get an element from a file */
  57. long DFputelement();    /* HDF call to put an element in a file */
  58. long unrleit();            /* call to un-run length encode a line of an image */
  59. int    DFsetfind();        /* HDF call to start a wildcard find operation */
  60. int    DFfind();            /* HDF call to perform the wildcard find */
  61. int    DFupdate();            /* HDF call to update a file after changes have been written */
  62. int    DFaccess();            /* HDF call to initiate reads or writes on a file */
  63. int    DFdup();            /* HDF call to create a new tag/ref indentical to an old one */
  64. int    DFclose();            /* HDF call to close an HDF file */
  65. extern int expand();    /* routine to expand a given box in the image to fill the screen */
  66. extern int interpolate();    /* routine to expand and smooth a given box in the image to fill the screen */
  67. char *malloc();            /* memory allocation procedure */
  68. char *calloc();            /* another memory allocation procedure */
  69. extern unsigned char pixelcolor();    /* returns the color of a pixel specified */
  70. extern unsigned char interdata();    /* returns the interpolated value in a box */
  71. DF    *DFopen();            /* HDF call to open a file and return a pointer to that file */
  72. void unimcomp();        /* call to uncompress an imcomp compressed line */
  73. void read_pic();        /* routine to read in an imcomp compressed HDF image in ega mode */
  74. void readpic();            /* routine to read in a raster image from an HDF file */
  75. void readcpic();        /* routine to read in a run-length-encoded image from an HDF file */
  76. void readipic();        /* routine to read in an imcomp compressed HDF image for other display modes */
  77. void loadega();            /* procedure to read in an HDF palette in ega mode */
  78. void select_pal();        /* procedure to select a palette of 16 colors out of 256 */
  79. void grafmode();        /* routine to go into the correct graphics mode */
  80. void textmode();        /* routine to go into text mode */
  81. void showpic();            /* routine to display the picture on the screen when in graphics mode */
  82. void loadpal();            /* procedure to load the palette for other display modes */
  83. extern void showpal();    /* routine to show the palette on the screen */
  84. extern void nopal();    /* routine to remove the palette from the screen */
  85. extern void options();    /* main routine to execute runtime options from */
  86. void putpal();            /* routine reload the internal palette which the values from current palette arrays */
  87. extern void findpal();    /* routine to get a new palette from an HDF file */
  88. void newpal();            /* routine to get a new palette in binary mode */
  89. void initpal();            /* procedure to load in the initial palette */
  90. void rawpic();            /* call to read in the binary image */
  91. extern void printdir();        /* routine to display a list of palettes in the current directory */
  92. extern void swap();            /* swap two character values */
  93. extern void slidepal();        /* slide the palette left or right */
  94. extern void squishpal();    /* compress or expand the palette */
  95. #ifdef MOUSE
  96. extern void mousefunc();    /* mouse functions for screen manipulation */
  97. #endif
  98. extern void updatescrn();        /* updates the screen with the image, color bar and mouse cursor */
  99. /*void t16line();*/                /* blast a targa line out to the targa board */
  100. /*void pline();*/
  101. /*void GraphInit();*/            /* routine in 'tarinit.c' to initialize the targa board */
  102. void makepal();            /* make a grey scale palette as the first palette */
  103. void displayerr();        /* display a string on the text screen */
  104. void usage();            /* display the command line parameters */
  105. void debug();            /* display debugging variables */
  106. void debugf();            /* display float debugging variables */
  107. extern void waitq();    /* routine to wait for a keypress */
  108. extern void bounce();    /* routine to bounce a ball on the screen until keyboard or mouse status is changed */
  109. extern void swapint();    /* routine to swap two integer values */
  110. extern void parse();    /* parse the commands generated by options() */
  111. void NO9setup();        /* number nine routines to initialize the screen */
  112. void clrbuf();
  113. void RG9gmode();
  114. void RG9clrscr();
  115. void RG9init();
  116.  
  117. main(argc,argv)        /* main routine */
  118.     int argc;        /* argument count */
  119.     char *argv[];    /* pointer to the argument list */
  120. {
  121.     mode=EGA;        /*default mode for displaying images */
  122.     maxx=640;
  123.     maxy=350;
  124.     pal_height=7;
  125.     if(argc<2){        /* if there are no command line parameters then print them */
  126.         usage();
  127.         exit(0);
  128.       }    /* end if */
  129.     for (i=1; i<argc; i++) {      /* look at each parm */
  130.         if (*argv[i] == '-') {
  131.             switch ( *(argv[i]+1)) { 
  132.                 case 'c':    /*  centering  */
  133.                     center=1;                /* set centering to calculate after the dimensions have been found */
  134.                     break;
  135.  
  136.                 case 'p':    /* special position */
  137.                     xwhere = atoi(argv[++i]);
  138.                     ywhere = atoi(argv[++i]);
  139.                     if (xwhere > maxx || ywhere > maxy) {
  140.                         puts("Invalid position\n");
  141.                         usage();
  142.                         exit(1);
  143.                       }    /* end if */
  144.                     break;
  145.  
  146.                 case 'h':    /* the file type specified in HDF */
  147.                     filetype='h';
  148.                     break;
  149.  
  150.                 case 'a':    /* set animation mode */
  151.                     animate=1;
  152.                     one_file=0;
  153.                     break;
  154.  
  155.                 case 'A':    /* set one file animation mode for hdf files */
  156.                     animate=1;
  157.                     one_file=1;
  158.                     break;
  159.  
  160.                 case 's':    /* set single binary image mode */
  161.                     oneimage=1;
  162.                     break;
  163.  
  164.                 case 'b':    /* the files are going to be binary raster 8 */
  165.                     filetype='b';
  166.                     xdim = atoi(argv[++i]);
  167.                     ydim = atoi(argv[++i]);
  168.                     if (xdim < 1 || ydim < 1) {
  169.                         puts("Invalid dimensions\n");
  170.                         usage();
  171.                         exit(1);
  172.                       } /* end if */
  173.                     break;
  174.  
  175.                 case VGA:    /* display mode is vga */
  176.                     mode=*(argv[i]+1);
  177.                     maxx=320;
  178.                     maxy=200;
  179.                     pal_height=7;
  180.                     break;
  181.  
  182.                 case EGA:    /* display mode is ega */
  183.                     mode=*(argv[i]+1);
  184.                     maxx=640;
  185.                     maxy=350;
  186.                     pal_height=7;
  187.                     break;
  188.  
  189.                  case TARGA:    /* display mode is t16 */
  190.                      mode=*(argv[i]+1);
  191.                      maxx=512;
  192.                      maxy=485;
  193.                      break;
  194.  
  195.                 case NO9:    /* display mode is no9 */
  196.                     mode=*(argv[i]+1);
  197.                     maxx=512;
  198.                     maxy=500;
  199.                     pal_height=15;
  200.                     break;
  201.  
  202.                 case 'm':    /* name of palette file for binary images */
  203.                     strcpy(palfile,argv[i+1]);
  204.                     break;
  205.  
  206.                 default:    /* not a valid command parameter, so tell them the parameters */
  207.                     usage();
  208.                     exit(1);
  209.               } /* end switch */
  210.           } /* end if */
  211.       } /* end for */
  212.     pal_yoff=0;
  213.  
  214. /* set the directory from the command */
  215.     getdir(old_path);                                /* get the old path for restoring later */
  216.     strsfn(argv[argc-1],drive,path,node,ext);        /* convert the filename on the command line into a drive, path, node, and extension */
  217.     printf("drive=%s\tpath=%s\tnode=%s\text=%s\n",drive,path,node,ext);
  218.     strcat(filename,drive);                            /* concatenate the drive and path names together for the setdir call */
  219.     strcat(filename,path);
  220.     if((i=strcmp(filename,""))!=0)        /* check for null directory */
  221.         if((i=setdir(filename))!=0) {        /* set the directory and check for a bad directory */
  222.             printf("Bad Path Specified, Error=%d\n",i);
  223.             exit(1);
  224.           }    /* end if */
  225.  
  226. /* start doing stuff with the image(s) */
  227.  
  228.     if(animate==1) {        /* do both hdf and raster animations here */
  229.         if(filetype=='h') {        /* do a hdf animation */
  230.             if(one_file==1) {    /* animate all the images from just one hdf file */
  231.                 grafmode();        /* go to correct graphics screen type and load palette */
  232.                 do {            /* cycle through until the user wants to get out */
  233.                     strcpy(filename,argv[argc-1]);        /* get a copy of the file to be read */
  234.                     file_attr=0;                        /* just look for normal files */
  235.                     file_err=find_1st(filename,file_attr);    /* get the first file to display */
  236.  
  237.                     do {                                /* display all the files */
  238.  
  239.                         strcpy(filename,DTAPTR+30);            /* get the filename from the find routine
  240.  
  241. /* Open DF file for reading */
  242.  
  243.                         if(NULL==(dff=DFopen(filename,DFACC_ALL,16))) {
  244.                             printf("Cannot open HDF file, HDF error:%d\n", DFerror);
  245.                             setdir(old_path);                /* restore the old path */
  246.                             exit(1);
  247.                           }    /* end if */
  248.  
  249. /* get the dimensions */
  250.  
  251.                         if(first_hdf==1) {
  252.                             DFsetfind(dff,DFTG_ID8,DFREF_WILDCARD);    /* set wildcard find to image dimension searching */
  253.  
  254.                             if(!DFfind(dff,&ddstr)) {            /* look for image dimensions */
  255.                                 if(0>DFgetelement(dff,ddstr.tag,ddstr.ref,&dims)) {
  256.                                     printf("Error getting dimensions from file: %d\n",DFerror);
  257.                                     setdir(old_path);                /* restore the old path */
  258.                                     exit(1);
  259.                                   }    /* end if */
  260.                               }    /* end if */
  261.                             else {
  262.                                 printf("error from DFfind %d\n",DFerror);
  263.                                 setdir(old_path);                /* restore the old path */
  264.                                 exit(1);
  265.                               }    /* end else */
  266.  
  267.                             xdim=intswap(dims.xdim);
  268.                             ydim=intswap(dims.ydim);
  269.  
  270.                             if(center) {                /* find the image's position on the screen */
  271.                                 xwhere=(maxx-xdim)/2;
  272.                                 ywhere=(maxy-ydim)/2;
  273.                                 if(xwhere<0) 
  274.                                     xwhere=0;            /* if larger than screen, */
  275.                                 if(ywhere<0)            /* don't center it */
  276.                                     ywhere=0;
  277.                               }    /* end if */
  278.  
  279. /* allocate memory for the image, if not enough, then error */
  280.  
  281.                             if(enoughspace(xdim,ydim)) {
  282.                                 printf(mem_error);
  283.                                 setdir(old_path);                /* restore the old path */
  284.                                 exit(1);
  285.                               } /* end if */
  286.                             first_hdf=0;                /* reset the first_hdf bit */
  287.                           }    /* end if */
  288.  
  289. /* cycle through all of the raster images in the file */
  290.  
  291.                         c=' ';
  292.                         cycled=0;
  293.                         DFsetfind(dff,DFTG_RI8,DFREF_WILDCARD);        /* set wildcard search for raster 8 images */
  294.     
  295.                         while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for the raster 8 images */
  296.                             imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  297.                             imref=ddstr.ref;
  298.                             readpic(xdim,ydim);        /* block read from disk */
  299.                             if(!cycled)
  300.                                 initpal();            /* loads in the palette from disk */
  301.                             showpic();        /* put the picture on the screen */
  302.                             cycled=1;        /* set flag to display that an animation has occured */
  303.                             if(kbhit()) {    /* check for keypress to interupt animation */
  304.                                 i=getch();    /* flush the keyboard buffer */
  305.                                 if(i==0)                /* get extended code if necessary */
  306.                                     i=getch();
  307.                                 if(show)        /* if palette is supposed to be display then display it again */
  308.                                     showpal();
  309.                                 options();
  310.                               }    /* end if */
  311.                           } /* end while */
  312.     
  313. /* cycle through all of the run length compressed images in the file */
  314.  
  315.                         c=' ';
  316.                         DFsetfind(dff,DFTG_CI8,DFREF_WILDCARD);
  317.  
  318.                         while(!DFfind(dff,&ddstr) && animate && !getout) {
  319.                             imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  320.                             imref=ddstr.ref;
  321.                                 readcpic(xdim,ydim);        /* block read run length compressed image from disk */
  322.                             if(!cycled)
  323.                                 initpal();            /* loads in the palette from disk */
  324.                             showpic();            /* put image on the screen */
  325.                             cycled=1;        /* set flag to display that an animation has occured */
  326.                             if(kbhit()) {    /* check for keypress to interupt animation */
  327.                                 i=getch();    /* flush the keyboard buffer */
  328.                                 if(i==0)                /* get extended code if necessary */
  329.                                     i=getch();
  330.                                 if(show)        /* if palette is supposed to be display then display it again */
  331.                                     showpal();
  332.                                 options();
  333.                               }    /* end if */
  334.                           } /* end while */
  335.  
  336. /* cycle through all of the imcomp compressed images in the file */
  337.  
  338.                         c=' ';
  339.                         DFsetfind(dff,DFTG_II8,DFREF_WILDCARD);    /* set wildcard search for imcomp compressed images */
  340.  
  341.                         while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for imcomp compressed images */
  342.                             imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  343.                             imref=ddstr.ref;
  344.                             if(!doneinit) {        /* if the palette hasn't been initialized */
  345.                                 initpal();        /* then initialize it for incomp compressed images */
  346.                                 doneinit=1;
  347.                               }    /* end if */
  348.                             if(mode==EGA) {            /* initialize things a certain way for ega mode */
  349.                                  read_pic(xdim,ydim);        /* block read from disk for ega mode */
  350.                                 select_pal(xdim,ydim,0);     /* set up the translation table for ega */
  351.                                 memcpy(egapals[0],regrs,16);
  352.                               }    /* end if */
  353.                             else
  354.                                 readipic(xdim,ydim);    /* block read from disk for other modes */
  355.                             showpic();            /* put the image on the screen */
  356.                             cycled=1;        /* set flag to display that an animation has occured */
  357.                             if(kbhit()) {    /* check for keypress to interupt animation */
  358.                                 i=getch();    /* flush the keyboard buffer */
  359.                                 if(i==0)                /* get extended code if necessary */
  360.                                     i=getch();
  361.                                 if(show)        /* if palette is supposed to be display then display it again */
  362.                                     showpal();
  363.                                 options();
  364.                               }    /* end if */
  365.                           } /* end while */
  366.                         DFclose(dff);            /* close the hdf file */
  367.                       }    while((file_err=findnext())==0);    /* keep reading files until an error is returned (no more files) */
  368.                     for(i=anispeed; i>0; i--);    /* delay loop */
  369.                     if(cycled) {
  370.                         showpic();
  371.                         if(show)        /* if palette is supposed to be display then display it again */
  372.                             showpal();
  373.                         options();
  374.                       }    /* end if */
  375.                   }    while(c!='q' && animate && cycled && !getout);    /* keep cycling until the user wants out */
  376.               }    /* end if */
  377.             else {            /* animate from a list of hdf files */
  378.                 grafmode();            /* go into correct graphics mode */
  379.                 c=' ';
  380.                 while(c!='q' && animate && !getout) {        /* open file with list of file names to display */
  381.                     if(NULL==(fp=fopen(argv[argc-1],"ra"))) {
  382.                         textmode();
  383.                         printf("\n%s: Error opening %s",argv[0],argv[argc-1]);
  384.                         setdir(old_path);                /* restore the old path */
  385.                         exit(0);
  386.                       } /* end if */
  387.     
  388.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  389.                         newpal(palfile);
  390.                     putpal();
  391.  
  392. /* cycle through all of the file names */
  393.  
  394.                     *filename='\0';
  395.  
  396.                     while((NULL!=fgets(filename,100,fp)) && animate && !getout) {
  397.                         if(*filename<33)
  398.                             break;                /* exit the program if end of list */
  399.                         p=filename;
  400.                         while(*p>32)            /*  find the first non printable char or space */
  401.                             p++;
  402.                         *p='\0';                /*  truncate the file name for use by open */
  403.  
  404.                         file_attr=0;                        /* just look for normal files */
  405.                         file_err=find_1st(filename,file_attr);    /* get the first file to display */
  406.  
  407.                         do {                                /* display all the files */
  408.  
  409.                             strcpy(filename,DTAPTR+30);            /* get the filename from the find routine
  410.  
  411. /* Open DF file for reading */
  412.  
  413.                             if(NULL==(dff=DFopen(filename,DFACC_ALL,16))) {
  414.                                 printf("Cannot open HDF file=%s, HDF error:%d\n",filename,DFerror);
  415.                                 setdir(old_path);                /* restore the old path */
  416.                                 exit(1);
  417.                               }    /* end if */
  418.  
  419.                             if(first_hdf==1) {        /* the first time through an hdf animation, get the dimensions and allocate spcae, etc. */
  420.  
  421. /* get the dimensions */
  422.  
  423.                                 DFsetfind(dff,DFTG_ID8,DFREF_WILDCARD);    /* set wildcard find to image dimension searching */
  424.  
  425.                                 if(!DFfind(dff,&ddstr)) {            /* look for image dimensions */
  426.                                     if(0>DFgetelement(dff,ddstr.tag,ddstr.ref,&dims)) {
  427.                                         printf("Error getting dimensions from file: %d\n",DFerror);
  428.                                         setdir(old_path);                /* restore the old path */
  429.                                         exit(1);
  430.                                       }    /* end if */
  431.                                   }    /* end if */
  432.                                 else {
  433.                                     printf("error from DFfind %d\n",DFerror);
  434.                                     setdir(old_path);                /* restore the old path */
  435.                                     exit(1);
  436.                                   }    /* end else */
  437.  
  438.                                 xdim=intswap(dims.xdim);
  439.                                 ydim=intswap(dims.ydim);
  440.  
  441.                                 if(center) {                /* find the image's position on the screen */
  442.                                     xwhere=(maxx-xdim)/2;
  443.                                     ywhere=(maxy-ydim)/2;
  444.                                     if(xwhere<0) 
  445.                                         xwhere=0;            /* if larger than screen, */
  446.                                     if(ywhere<0)            /* don't center it */
  447.                                         ywhere=0;
  448.                                   }    /* end if */
  449.  
  450. /* allocate memory for the image, if not enough, then error */
  451.  
  452.                                 if(enoughspace(xdim,ydim)) {
  453.                                     printf(mem_error);
  454.                                     setdir(old_path);                /* restore the old path */
  455.                                     exit(1);
  456.                                   } /* end if */
  457.  
  458.                                 first_hdf=0;
  459.                               }    /* end if */
  460.  
  461. /* cycle through all of the raster images in the file */
  462.  
  463.                             c=' ';
  464.                             cycled=0;
  465.                             DFsetfind(dff,DFTG_RI8,DFREF_WILDCARD);        /* set wildcard search for raster 8 images */
  466.  
  467.                             while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for the raster 8 images */
  468.                                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  469.                                 imref=ddstr.ref;
  470.                                 readpic(xdim,ydim);        /* block read from disk */
  471.                                 if(!cycled)
  472.                                     initpal();            /* loads in the palette from disk */
  473.                                 showpic();        /* put the picture on the screen */
  474.                                 cycled=1;        /* set flag to display that an animation has occured */
  475.                                 if(kbhit()) {    /* check for keypress to interupt animation */
  476.                                     i=getch();    /* flush the keyboard buffer */
  477.                                     if(i==0)                /* get extended code if necessary */
  478.                                         i=getch();
  479.                                     if(show)        /* if palette is supposed to be display then display it again */
  480.                                         showpal();
  481.                                     options();
  482.                                   }    /* end if */
  483.                               } /* end while */
  484.  
  485. /* cycle through all of the run length compressed images in the file */
  486.  
  487.                             c=' ';
  488.                             DFsetfind(dff,DFTG_CI8,DFREF_WILDCARD);
  489.  
  490.                             while(!DFfind(dff,&ddstr) && animate && !getout) {
  491.                                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  492.                                 imref=ddstr.ref;
  493.                                 readcpic(xdim,ydim);        /* block read run length compressed image from disk */
  494.                                 if(!cycled)
  495.                                 initpal();            /* loads in the palette from disk */
  496.                                 showpic();            /* put image on the screen */
  497.                                 cycled=1;        /* set flag to display that an animation has occured */
  498.                                 if(kbhit()) {    /* check for keypress to interupt animation */
  499.                                     i=getch();    /* flush the keyboard buffer */
  500.                                     if(i==0)                /* get extended code if necessary */
  501.                                         i=getch();
  502.                                     if(show)        /* if palette is supposed to be display then display it again */
  503.                                         showpal();
  504.                                     options();
  505.                                   }    /* end if */
  506.                               } /* end while */
  507.  
  508. /* cycle through all of the imcomp compressed images in the file */
  509.  
  510.                             c=' ';
  511.                             DFsetfind(dff,DFTG_II8,DFREF_WILDCARD);    /* set wildcard search for imcomp compressed images */
  512.  
  513.                             while(!DFfind(dff,&ddstr) && animate && !getout) {        /* look for imcomp compressed images */
  514.                                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  515.                                 imref=ddstr.ref;
  516.                                 if(!doneinit) {        /* if the palette hasn't been initialized */
  517.                                     initpal();        /* then initialize it for incomp compressed images */
  518.                                     doneinit=1;
  519.                                   }    /* end if */
  520.  
  521.                                 if(mode==EGA) {            /* initialize things a certain way for ega mode */
  522.                                      read_pic(xdim,ydim);        /* block read from disk for ega mode */
  523.                                     select_pal(xdim,ydim,0);     /* set up the translation table for ega */
  524.                                     memcpy(egapals[0],regrs,16);
  525.                                   }    /* end if */
  526.                                 else
  527.                                     readipic(xdim,ydim);    /* block read from disk for other modes */
  528.                                 showpic();            /* put the image on the screen */
  529.                                 cycled=1;        /* set flag to display that an animation has occured */
  530.                                 if(kbhit()) {    /* check for keypress to interupt animation */
  531.                                     i=getch();    /* flush the keyboard buffer */
  532.                                     if(i==0)                /* get extended code if necessary */
  533.                                         i=getch();
  534.                                     if(show)        /* if palette is supposed to be display then display it again */
  535.                                         showpal();
  536.                                     options();
  537.                                   }    /* end if */
  538.                               } /* end while */
  539.                             DFclose(dff);            /* close the hdf file */
  540.                           }    while((file_err=findnext())==0);    /* keep going until no more files with that name */
  541.                       }    /* end while */
  542.                     for(i=anispeed; i>0; i--);    /* delay loop */
  543.                     if(cycled) {
  544.                         showpic();
  545.                         if(show)        /* if palette is supposed to be display then display it again */
  546.                             showpal();
  547.                         options();
  548.                       }    /* end if */
  549.                   }    /* end while */
  550.               }    /* end else */
  551.           }    /* end if */
  552.         else {                    /* do a raster eight animation */
  553.  
  554. /* allocate memory for the image, if not enough, then error */
  555.  
  556.             if(enoughspace(xdim,ydim)) {
  557.                 printf(mem_error);
  558.                 setdir(old_path);                /* restore the old path */
  559.                 exit(1);
  560.               }    /* end if */
  561.  
  562.             if(center) {                /* find the image's position on the screen */
  563.                 xwhere=(maxx-xdim)/2;
  564.                 ywhere=(maxy-ydim)/2;
  565.                 if(xwhere<0) 
  566.                     xwhere=0;            /* if larger than screen, */
  567.                 if(ywhere<0)            /* don't center it */
  568.                     ywhere=0;
  569.               }    /* end if */
  570.               
  571.             grafmode();            /* go into correct graphics mode */
  572.  
  573. /* open file with list of file names to display */
  574.  
  575.             if(one_file==1) {    /* if there is only one file name specified for the animation */
  576.                 c=' ';
  577.                 while(c!='q' && animate && !getout) {
  578.  
  579.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  580.                         newpal(palfile);
  581.                     putpal();
  582.  
  583. /* cycle through all of the file names */
  584.  
  585.                     file_attr=0;            /* look for normal files */
  586.                     file_err=find_1st(argv[argc-1],file_attr);    /* get the name of the first file which matches */
  587.  
  588.                     do {
  589.                         strcpy(filename,DTAPTR+30);
  590.  
  591.                         if(0>(file=open(filename,O_RDONLY | O_RAW))) {        /* open the image file */
  592.                             textmode();
  593.                             printf("\n%s: Error opening image file: %s",argv[0],filename);
  594.                             setdir(old_path);                /* restore the old path */
  595.                             exit(1);
  596.                           }    /* end if */
  597.                         rawpic(file,xdim,ydim);            /* block read image from from disk */
  598.                         close(file);                    /* close the image file */
  599.                         showpic();            /* put the image on the screen */
  600.                         if(kbhit()) {    /* check for keypress to interupt animation */
  601.                             i=getch();    /* flush the keyboard buffer */
  602.                             if(i==0)                /* get extended code if necessary */
  603.                                 i=getch();
  604.                             if(show)        /* if palette is supposed to be display then display it again */
  605.                                 showpal();
  606.                             options();
  607.                           }    /* end if */
  608.                         for(i=anispeed; i>0; i--);    /* delay loop */
  609.                       }    while((file_err=findnext())==0);    /* keep cycling through the files until all the filenames are exhausted */
  610.                     showpic();
  611.                     if(show)        /* if palette is supposed to be display then display it again */
  612.                         showpal();
  613.                     options();            /* do the fancy things on the screen */
  614.                   }    /* end while */
  615.               }    /* end if */
  616.             else {                /* animate from a file of filenames */
  617.                 c=' ';
  618.                 while(c!='q' && animate && !getout) {
  619.                     if(NULL==(fp=fopen(argv[argc-1],"ra"))) {
  620.                         printf("\n%s: Error opening %s",argv[0],argv[argc-1]);
  621.                         setdir(old_path);                /* restore the old path */
  622.                         exit(0);
  623.                       } /* end if */
  624.  
  625.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  626.                         newpal(palfile);
  627.                     putpal();
  628.  
  629. /* cycle through all of the file names */
  630.  
  631.                     *filename='\0';
  632.  
  633.                     while((NULL!=fgets(filename,100,fp)) && animate && !getout) {
  634.                         if(*filename<33)
  635.                             break;                /* exit the program if end of list */
  636.                         p=filename;
  637.                         while(*p>32)            /*  find the first non printable char or space */
  638.                             p++;
  639.                         *p='\0';                /*  truncate the file name for use by open */
  640.  
  641.                         file_attr=0;            /* look for normal files */
  642.                         file_err=find_1st(filename,file_attr);    /* get the name of the first file which matches */
  643.  
  644.                         do {
  645.                             strcpy(filename,DTAPTR+30);
  646.  
  647.                             if(0>(file=open(filename,O_RDONLY | O_RAW))) {        /* open the image file */
  648.                                 textmode();
  649.                                 printf("\n%s: Error opening image file: %s",argv[0],filename);
  650.                                 setdir(old_path);                /* restore the old path */
  651.                                 exit(1);
  652.                               }    /* end if */
  653.                             rawpic(file,xdim,ydim);            /* block read image from from disk */
  654.                             close(file);                    /* close the image file */
  655.                             showpic();            /* put the image on the screen */
  656.                             if(kbhit()) {    /* check for keypress to interupt animation */
  657.                                 i=getch();    /* flush the keyboard buffer */
  658.                                 if(i==0)                /* get extended code if necessary */
  659.                                     i=getch();
  660.                                 if(show)        /* if palette is supposed to be display then display it again */
  661.                                     showpal();
  662.                                 options();
  663.                               }    /* end if */
  664.                             for(i=anispeed; i>0; i--);    /* delay loop */
  665.                           }    while((file_err=findnext())==0);    /* keep cycling through the files until all the filenames are exhausted */
  666.                       } /* end while */
  667.                     showpic();
  668.                     if(show)        /* if palette is supposed to be display then display it again */
  669.                         showpal();
  670.                     options();            /* do the fancy things on the screen */
  671.                   }    /* end while */
  672.               }    /* end else */
  673.           }    /* end else */
  674.         textmode();
  675.         setdir(old_path);                /* restore the old path */
  676.         exit(0);    /* end program if no more images */
  677.       }    /* end if */
  678.     else {    /* regular non-animation file loading */
  679.         if(filetype=='h') {        /* hdf files specified */
  680.  
  681. /* Open DF file for reading */
  682.  
  683.             if(NULL==(dff=DFopen(argv[argc-1],DFACC_ALL,16))) {
  684.                 printf("Cannot open HDF file, HDF error:%d\n", DFerror);
  685.                 setdir(old_path);                /* restore the old path */
  686.                 exit(1);
  687.               }    /* end if */
  688.  
  689. /* get the dimensions */
  690.  
  691.             DFsetfind(dff,DFTG_ID8,DFREF_WILDCARD);    /* set wildcard find to image dimension searching */
  692.  
  693.             if(!DFfind(dff,&ddstr)) {            /* look for image dimensions */
  694.                 if(0>DFgetelement(dff,ddstr.tag,ddstr.ref,&dims)) {
  695.                     printf("Error getting dimensions from file: %d\n",DFerror);
  696.                     setdir(old_path);                /* restore the old path */
  697.                     exit(1);
  698.                   }    /* end if */
  699.               }    /* end if */
  700.             else{
  701.                 printf("error from DFfind %d\n",DFerror);
  702.                 setdir(old_path);                /* restore the old path */
  703.                 exit(1);
  704.               }    /* end else */
  705.         
  706.             xdim=intswap(dims.xdim);
  707.             ydim=intswap(dims.ydim);
  708.  
  709.             if(center) {                /* find the image's position on the screen */
  710.                 xwhere=(maxx-xdim)/2;
  711.                 ywhere=(maxy-ydim)/2;
  712.                 if(xwhere<0) 
  713.                     xwhere=0;            /* if larger than screen, */
  714.                 if(ywhere<0)            /* don't center it */
  715.                     ywhere=0;
  716.               }    /* end if */
  717.  
  718. /* allocate memory for the image, if not enough, then error */
  719.  
  720.             if(enoughspace(xdim,ydim)) {
  721.                 printf(mem_error);
  722.                 setdir(old_path);                /* restore the old path */
  723.                 exit(1);
  724.               } /* end if */
  725.  
  726.             grafmode();        /* go to correct graphics screen type and load palette */
  727.  
  728. /* cycle through all of the raster images in the file */
  729.  
  730.             DFsetfind(dff,DFTG_RI8,DFREF_WILDCARD);        /* set wildcard search for raster 8 images */
  731.  
  732.             while(!DFfind(dff,&ddstr) && !getout) {        /* look for the raster 8 images */
  733.                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  734.                 imref=ddstr.ref;
  735.                 readpic(xdim,ydim);        /* block read from disk */
  736.                 initpal();            /* loads in the palette from disk */
  737.                 showpic();        /* put the picture on the screen */
  738.                 if(show)        /* if palette is supposed to be display then display it again */
  739.                     showpal();
  740.                 options();        /* call routine for screen options */
  741.               } /* end while */
  742.  
  743. /* cycle through all of the run length compressed images in the file */
  744.  
  745.             DFsetfind( dff, DFTG_CI8, DFREF_WILDCARD);
  746.  
  747.             while(!DFfind( dff, &ddstr) && !getout) {
  748.                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  749.                 imref=ddstr.ref;
  750.                  readcpic(xdim,ydim);        /* block read run length compressed image from disk */
  751.                 initpal();            /* loads in the palette from disk */
  752.                 showpic();            /* put image on the screen */
  753.                 if(show)        /* if palette is supposed to be display then display it again */
  754.                     showpal();
  755.                 options();            /* call routine for screen options */
  756.               } /* end while */
  757.  
  758. /* cycle through all of the imcomp compressed images in the file */
  759.  
  760.             DFsetfind(dff,DFTG_II8,DFREF_WILDCARD);    /* set wildcard search for imcomp compressed images */
  761.  
  762.             while(!DFfind(dff,&ddstr) && !getout) {        /* look for imcomp compressed images */
  763.                 imtag=ddstr.tag;        /* keep track of the image's tag and reference numbers for later */
  764.                 imref=ddstr.ref;
  765.                 initpal();                /* neccessary for imcomp compressed images */
  766.                 if(mode==EGA){            /* initialize things a certain way for ega mode */
  767.                      read_pic(xdim,ydim);        /* block read from disk for ega mode */
  768.                     select_pal(xdim,ydim,0);     /* set up the translation table for ega */
  769.                     memcpy(egapals[0],regrs,16);
  770.                   }    /* end if */
  771.                 else
  772.                     readipic(xdim,ydim);    /* block read from disk for other modes */
  773.                 showpic();            /* put the image on the screen */
  774.                 if(show)        /* if palette is supposed to be display then display it again */
  775.                     showpal();
  776.                 options();            /* call routine for screen options */
  777.               } /* end while */
  778.             DFclose(dff);            /* close the hdf file */
  779.           } /* end if */
  780.         else {        /* do raster images */
  781.  
  782. /* allocate memory for the image, if not enough, then error */
  783.  
  784.             if(enoughspace(xdim,ydim)) {
  785.                 printf(mem_error);
  786.                 setdir(old_path);                /* restore the old path */
  787.                 exit(1);
  788.               }    /* end if */
  789.  
  790.             if(center) {                /* find the image's position on the screen */
  791.                 xwhere=(maxx-xdim)/2;
  792.                 ywhere=(maxy-ydim)/2;
  793.                 if(xwhere<0) 
  794.                     xwhere=0;            /* if larger than screen, */
  795.                 if(ywhere<0)            /* don't center it */
  796.                     ywhere=0;
  797.               }    /* end if */
  798.  
  799. /* open file with list of file names to display */
  800.             if(oneimage) {    /* only read in one image and display it */
  801.                 if(0>(file=open(argv[argc-1],O_RDONLY|O_RAW))) {        /* open the image file */
  802.                     printf("\n%s: Error opening image file: %s",argv[0],argv[argc-1]);
  803.                     setdir(old_path);                /* restore the old path */
  804.                     exit(0);
  805.                   }    /* end if */
  806.                 rawpic(file,xdim,ydim);            /* block read image from from disk */
  807.                 close(file);                    /* close the image file */
  808.                 grafmode();            /* go into correct graphics mode */
  809.                 newpal(palfile);    /* load the new palette */
  810.                 putpal();
  811.                 showpic();            /* put the image on the screen */
  812.                 options();            /* do the fancy things on the screen */
  813.               }    /* end if */
  814.             else {        /* read in a list of image names to display */
  815.                 if(NULL==(fp=fopen(argv[argc-1],"ra"))) {
  816.                     printf("\n%s: Error opening %s",argv[0],argv[argc-1]);
  817.                     setdir(old_path);                /* restore the old path */
  818.                     exit(1);
  819.                   } /* end if */
  820.  
  821. /* cycle through all of the file names */
  822.  
  823.                 *filename='\0';
  824.  
  825.                 while(NULL!=fgets(filename,100,fp) && !getout) {
  826.                     if(*filename<33)
  827.                         break;                /* exit the program if end of list */
  828.                     p=filename;
  829.                     while(*p>32)            /*  find the first non printable char or space */
  830.                         p++;
  831.                     *p='\0';                /*  truncate the file name for use by open */
  832.                     if(0>(file=open(filename,O_RDONLY|O_RAW))) {        /* open the image file */
  833.                         textmode();
  834.                         printf("\n%s: Error opening image file: %s",argv[0],filename);
  835.                         setdir(old_path);                /* restore the old path */
  836.                         exit(1);
  837.                       }    /* end if */
  838.                     rawpic(file,xdim,ydim);            /* block read image from from disk */
  839.                     close(file);                    /* close the image file */
  840.                     grafmode();                        /* go into correct graphics mode */
  841.                     if(palmax==-1 && *palfile!=NULL)    /* if there is a palette specified then load it */
  842.                         newpal(palfile);
  843.                     putpal();
  844.                     showpic();            /* put the image on the screen */
  845.                     options();            /* do the fancy things on the screen */
  846. /*
  847. *  insert each image functions here(click or gwrite)
  848. */
  849. /*             gwrite(filename); */
  850.                   } /* end while */
  851.               }    /* end else */
  852.           }    /* end else */
  853.  
  854.         textmode();
  855.         setdir(old_path);                /* restore the old path */
  856.         exit(0);    /* end program if no more images */
  857.       } /* end else */
  858.     return(0);
  859. } /* end main */
  860.  
  861. /**********************************************************************
  862. *  Function    :    enoughspace
  863. *  Purpose    :    make enough room for the maximum row and column size.
  864. *            Image memory is allocated line by line because of 64k data
  865. *            limitation of the PC.
  866. *  Parameters    :
  867. *            c - the number of columns in the image
  868. *            l - the number of lines in the image
  869. *  Returns    :
  870. *            1 if not enough memory
  871. *            0 if enough memory
  872. *  Calls    :    max()
  873. *  Called by    :    main()
  874. **********************************************************************/
  875. int enoughspace(c,l)
  876. int l,    /* the number of lines in image */
  877.     c;    /* the number of columns in the image */
  878. {
  879.     int i;    /* local counting variable */
  880.  
  881.        for (i=0; i < l; i++) {                /* allocate each line separately */
  882.         store[i] = malloc(max(c,256)); /* with a minimum of 256 pixels per line allocated */
  883.  
  884.         if (store[i] == NULL)           /* malloc returned not enough */
  885.             return(1);
  886.     }    /* end for */
  887.  
  888.     return(0);                             /* successful return */
  889.  
  890. } /* end enoughspace() */
  891.  
  892. /**********************************************************************
  893. *  Function    :    rawpic
  894. *  Purpose    :    block read the binary image from disk and copy to main memory
  895. *                    storage.
  896. *  Parameters    :
  897. *            file - the file pointer
  898. *            len     - the length of a line (xdim)
  899. *            lines - the number of lines (ydim)
  900. *  Returns    :    none
  901. *  Calls    :    none
  902. *  Called by    :    main()
  903. **********************************************************************/
  904. void rawpic(file,len,lines)
  905. int file,    /* the file pointer */
  906.     len,    /* the length of a line of the binary image */
  907.     lines;    /* the number of lines in the image */
  908. {
  909.     int i,    /* local temporary counting variable */
  910.         lno,    /* the current line number */
  911.         readsize,    /* the size to block read */
  912.         readfact;    /* the block reading factor */
  913.     char *p;    /* pointer to the buffer */
  914.  
  915.     readfact = RAWSIZE/len - 5;
  916.     readsize =  readfact*len;      /* get good blocking factor */
  917.  
  918.     lno = 0;
  919.     do {
  920.         read(file,raw,readsize);        /* read one block */
  921.         p = raw;                        /* point to that block */
  922.  
  923. /*
  924. *  for this block, copy each line into it's own reserved memory
  925. */
  926.         for (i=0; i < readfact && lno < lines; i++) {
  927.             memcpy(store[lno++],p,len);
  928.             p += len;
  929.         } /* end for */
  930.  
  931.     } while (lno < lines);
  932.  
  933. } /* end rawpic() */
  934.  
  935. /**********************************************************************
  936. *  Function    :    readpic
  937. *  Purpose    :    block read the image from the HDf file into main memory
  938. *                    storage
  939. *  Parameters    :
  940. *            len - the length of a line (xdim)
  941. *            lines - the number of lines (ydim)
  942. *  Returns    :    none
  943. *  Calls    :    DFaccess(), DFread()
  944. *  Called by    :    main()
  945. **********************************************************************/
  946. void readpic(len,lines)
  947. int len,    /* the length of a line */
  948.     lines;    /* the number of lines */
  949. {
  950.     int i,j,    /* local temporary counting variable */
  951.         lno,    /* the current line number */
  952.         readsize,    /* the size to block read */
  953.         readfact;    /* the block reading factor */
  954.     char *p;    /* pointer to the buffer */
  955.  
  956. /* prime DF routines */
  957.  
  958.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");    /* tell HDF to start reading */
  959.  
  960.     readfact = RAWSIZE/len - 5;
  961.     readsize =  readfact*len;      /* get good blocking factor */
  962.     lno = 0;
  963.     do {
  964.         j=(int) DFread(dff,raw,(long)readsize);        /* read one block */
  965.         p = raw;                        /* point to that block */
  966.  
  967. /* for this block, copy each line into it's own reserved memory */
  968.  
  969.         for (i=0; i*len < j && lno < lines; i++) {
  970.             memcpy(store[lno++],p,len);
  971.             p += len;
  972.           } /* end for */
  973.       } while (lno < lines);
  974. } /* end readpic() */
  975.  
  976. /**********************************************************************
  977. *  Function    :    readcpic
  978. *  Purpose    :    bloack read a run-length compressed image from an HDF
  979. *            file and store it in main memory, doing run length 
  980. *            decompression as it goes
  981. *  Parameters    :
  982. *            len - the length of a line of the image (xdim)
  983. *            lines - the number of lines in the image (ydim)
  984. *  Returns    :    none
  985. *  Calls    :    DFaccess(),DFread(),unrleit()
  986. *  Called by    :    main()
  987. **********************************************************************/
  988. void readcpic(len,lines)
  989. int len,    /* the length of a line */
  990.     lines;    /* the number of lines */
  991. {
  992.     int    i;    /* temporary counting variable */
  993.     long n,    /* the number of bytes read into the buffer */
  994.         bufleft,    /* the number of bytes left in the buffer */
  995.         crowsize,    /* the maximum compressed row size */
  996.         readsize,    /* the size of the block to read in */
  997.         totalread;    /* also the number of bytes read into the buffer */
  998.     unsigned char *in;    /* pointer to the buffer for data */
  999.  
  1000. /* prime DF routines */
  1001.  
  1002.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");    /* set HDF to begin reading */
  1003.  
  1004.     readsize = RAWSIZE - 16;
  1005.     in = raw;
  1006.     n=DFread(dff, in,readsize);        /* block read in data from HDF file */
  1007.     totalread = n;
  1008.     crowsize=(long)xdim*121/120 +1;        /* maximum rowsize of compressed data */
  1009.     bufleft = n;
  1010.     for (i=0; i<ydim; i++) {        /* uncompress until end of file or buffer */
  1011.         n = unrleit(in, store[i], (long) xdim);
  1012.         in += n;
  1013.         bufleft -= n;
  1014.         if (bufleft<crowsize) {        /* if row is chopped then read more in */
  1015.             memcpy(raw, in, bufleft);
  1016.             in = raw;
  1017.             n=DFread(dff,&in[bufleft],readsize-bufleft);
  1018.             totalread += n;
  1019.             bufleft += n;
  1020.         } /* end if */
  1021.     } /* end for */
  1022. } /* end readcpic() */
  1023.  
  1024. /**********************************************************************
  1025. *  Function    :    readipic
  1026. *  Purpose    :    block read in HDF image and do imcomp decompression as
  1027. *            it goes
  1028. *  Parameters    :
  1029. *            len - the length of a line (xdim)
  1030. *            lines - the number of lines in image (ydim)
  1031. *  Returns    :    none
  1032. *  Calls    :    DFaccess(), DFread(), unimcomp(), memcpy()
  1033. *  Called by    :    main()
  1034. **********************************************************************/
  1035. void readipic(len,lines)
  1036. int len,    /* the length of a line of the image */
  1037.     lines;    /* the number of lines in the image */
  1038. {
  1039.     int i,    /* local temporary counting variable */
  1040.         lno,    /* the current line number */
  1041.         readsize,    /* the size to block read */
  1042.         readline,    /* the block reading factor */
  1043.         itread;        /* the number of bytes read in */
  1044.     char *p,    /* pointer to the buffer */
  1045.         *endp;    /* pointer to the end of the buffer */
  1046.  
  1047. /* prime DF routines */
  1048.  
  1049.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");
  1050.  
  1051.     readline=RAWSIZE /xdim -5;
  1052.     readsize = readline*xdim;    
  1053.     lno = 0;
  1054.     do {
  1055.         itread = DFread(dff,raw,(long)readsize);    /* block read in imcomp compressed image */
  1056.         p = raw;
  1057.         endp = raw + itread;
  1058.  
  1059. /* take each imcomp compressed line and uncompress it */
  1060.  
  1061.         while(p<endp && lno<(ydim/4)){
  1062.             unimcomp(xdim,1,p,bigstore);
  1063.             for (i=0;i<4 ;i++)
  1064.                 memcpy(store[lno*4+i],&bigstore[i*xdim],xdim);
  1065.             p += xdim;
  1066.             lno++;
  1067.           } /* end while */
  1068.     } while (lno<(ydim/4));
  1069. } /* end readipic() */
  1070.  
  1071. /**********************************************************************
  1072. *  Function    : read_pic
  1073. *  Purpose    : Reads in the raw image in big blocks from an HDF file
  1074. *                Performs decompression and store it in array 'store'
  1075. *  Parameters    :
  1076. *    xdim,ydim  - x and y dimension of image
  1077. *  Returns    : none
  1078. *  Calls    : DFaccess(), DFread(), unimcomp(), choose_rgb()
  1079. *  Called by    : main()
  1080. **********************************************************************/
  1081. void read_pic(xdim,ydim)
  1082. int xdim, ydim;    /* x & y dimensions of the image to be read in */
  1083. {
  1084.     int i,j,k,cnt,    /* more local counting variables */
  1085.         readline,    /* the number of lines the storage array can hold */
  1086.         readsize,    /* the size of the block to be read in */
  1087.         nlines;        /* the current line count into the image */
  1088.     char *p;        /* character pointer to the beginning of the array read in */
  1089.     unsigned char *out,        /* character pointer to the unimcomp compressed lines */
  1090.                      color; /* color value for a pixel */
  1091.     void unimcomp();    /* routine to uncompress imcomp line compression */
  1092.     int choose_rgb();    /* routine to get 6 bit color values from 24 bit palettes */
  1093.  
  1094.     for (j=0; j<64; j++)    /* clear out the frequency count array */
  1095.         count[j] = 0;
  1096.     
  1097. /* read in image */
  1098.  
  1099.     DFaccess( dff, ddstr.tag, ddstr.ref, "r");
  1100.  
  1101.     readline = RAWSIZE / xdim - 5;
  1102.     readsize = readline * xdim;
  1103.     out = (unsigned char *)malloc(4*xdim);
  1104.     nlines = 0;
  1105.     do {
  1106.         cnt = DFread(dff,raw,(long)readsize);    /* get a chunk of the image */
  1107.     
  1108. /* count the frequency of colors */
  1109.  
  1110.         for (i=0; i<cnt; i=i+4)
  1111.            for (j=HI; j<=LO; j++) {
  1112.                color = raw[i+j];
  1113.                k = 3 * color;
  1114.                count[choose_rgb(pal[k],pal[k+1],pal[k+2])]++;    /* get the correct color setting and increment it */
  1115.               } /* end of for j */
  1116.         
  1117. /* decompress the raw image */
  1118.  
  1119.         for (j=0; j<readline; j++) {
  1120.             unimcomp(xdim,1,&(raw[xdim*j]),out);    /* uncompress each compressed line into four normal lines */
  1121.             p = out;
  1122.             for (i=0; i< 4 && nlines < ydim; i++) {    /* copy the four lines into memory */
  1123.                 memcpy(store[nlines],p,xdim);
  1124.                 nlines++;
  1125.                 p = p + xdim;
  1126.                 }    /* end of for i */
  1127.           }    /* end of for j */
  1128.     } while (nlines < ydim);
  1129. } /* end of read_pic() */
  1130.  
  1131. /**********************************************************************
  1132. *  Function    :    palloc
  1133. *  Purpose    :    allocate room in dynamic memory for a 256 byte palette
  1134. *  Parameters    :    
  1135. *            x - the number of the palette to try to allocate
  1136. *  Returns    :
  1137. *            1 if a palette was allocated
  1138. *            0 if no memory
  1139. *  Calls    :    none
  1140. *  Called by    :    loadpal(), findpal(), newpal()
  1141. **********************************************************************/
  1142. int palloc(x)
  1143. int x;    /* the number of the palette to try to allocate */
  1144. {
  1145.     if(x<16){    /* the legal number of palettes in memory */
  1146.         rpal[x]=malloc(256);
  1147.         gpal[x]=malloc(256);
  1148.         bpal[x]=malloc(256);
  1149.  
  1150.         if (rpal[x]==NULL || gpal[x]==NULL || bpal[x]==NULL)    /* if any of the palettes were not able to be allocated then return 0 */
  1151.             return(0);
  1152.         else{                /* otherwise, copy in the current palette */
  1153.             memcpy(rpal[x],rmap,256);
  1154.             memcpy(gpal[x],gmap,256);
  1155.             memcpy(bpal[x],bmap,256);
  1156.             return(1);
  1157.           }    /* end else */
  1158.       }    /* end if */
  1159.     else
  1160.         return(0);                /* if too many palettes then return 0 */
  1161. } /* end palloc() */
  1162.  
  1163. /**********************************************************************
  1164. *  Function    :    palloce
  1165. *  Purpose    :    allocate room for an ega palette and copy the current
  1166. *            one into that memory
  1167. *  Parameters    :
  1168. *            x - the number of the palette to allocate memory for
  1169. *  Returns    :
  1170. *            1 if the memory was allocated ok
  1171. *            0 if the memory was not available
  1172. *  Calls    :    none
  1173. *  Called by    :    loadega(), findpal(), newpal()
  1174. **********************************************************************/
  1175. int palloce(x)
  1176. int x;    /* the number of the palette to be allocated */
  1177. {
  1178.     if(x<16){    /* if it is a legal palette value */
  1179.         egapals[x]=malloc(16);
  1180.     
  1181.         if (egapals[x]==NULL)    /* if no memory available, then return 0 */
  1182.             return(0);
  1183.         else{                    /* otherwise, return 1 and copy the palette into the new memory */
  1184.             memcpy(egapals[x],regrs,16);
  1185.             return(1);
  1186.           }    /* end else */
  1187.       } /* end if */
  1188.     else
  1189.         return(0);                /* return 0 if too many palettes */
  1190. } /* end palloce() */
  1191.  
  1192. /**********************************************************************
  1193. *  Function    :    loadpal
  1194. *  Purpose    :    load in a palette from an HDF file and set the vga or
  1195. *            no. 9 palette to it.
  1196. *  Parameters    :    none
  1197. *  Returns    :    none
  1198. *  Calls    :    DFsetfind(), DFfind, DFgetelement(), palloc(), putmap9(),
  1199. *             putmapv()
  1200. *  Called by    :    initpal()
  1201. **********************************************************************/
  1202. void loadpal()
  1203. {
  1204.     int i;    /* local counting variable */
  1205.     char *pi;    /* pointer to the chracter array read in */
  1206.  
  1207.     DFgetelement( dff, DFTG_IP8, imref, pal);    /* get palette associated with the image */
  1208.  
  1209.     pi = pal;
  1210.     for (i=0; i<256; i++) {
  1211.         rmap[i] = *pi++;
  1212.         gmap[i] = *pi++;
  1213.         bmap[i] = *pi++;
  1214.     } /* end for */
  1215.  
  1216.     palnum=0;
  1217.     palmax=0;
  1218.     if(!palloc(palmax)) {    /* allocate room for another palette */
  1219.         palmax=-1;        /* bad allocation */
  1220.         displayerr("No Room For Another Palette");
  1221.       }    /* end if */
  1222.     else                /* ok allocation for palette */
  1223.         switch (mode) {        /* switch routine to put the correct palette */
  1224.             case VGA:
  1225.                 putmapv(&rmap[0],&gmap[0],&bmap[0]);
  1226.                 break;
  1227.  
  1228.             case NO9:
  1229.                 putmap9(&rmap[0],&gmap[0],&bmap[0]);
  1230.                 break;
  1231.           }    /* end switch */
  1232. } /* end loadpal() */
  1233.  
  1234. /**********************************************************************
  1235. *  Function    :    loadega
  1236. *  Purpose    :    load in the ega palette from an HDF file
  1237. *  Parameters    :    none
  1238. *  Returns    :    none
  1239. *  Calls    :    DFsetfind(), DFfind(), DFgetelement(), select_pal(),
  1240. *            palloce()
  1241. *  Called by    :    initpal()
  1242. **********************************************************************/
  1243. void loadega()
  1244. {
  1245.     int    i;    /* local counting variable */
  1246.  
  1247.     DFgetelement( dff, DFTG_IP8, imref, pal);    /* get palette associated with the image */
  1248.  
  1249.     palnum=0;
  1250.     palmax=0;
  1251.     select_pal(xdim,ydim,1);    /* perform a frequency count on it */
  1252.     if(!palloce(palmax)) {    /* allocate room for the first palette */
  1253.         palmax=-1;
  1254.         displayerr("No Room For Another Palette");
  1255.       }    /* end if */
  1256.     else 
  1257.         for(i=0; i<16; i++)
  1258.             egapal(i,(int)regrs[i]);
  1259. } /* end loadega() */
  1260.  
  1261. /**********************************************************************
  1262. *  Function    :    unrleit
  1263. *  Purpose    :    decompress run length encoding of a line of an image
  1264. *  Parameters    :
  1265. *            buf - the character string to be unencoded
  1266. *            bufto - the character string to be unencoded into
  1267. *            outlen - the length of the final, unencoded string
  1268. *  Returns    :    the length of the encoded line
  1269. *  Calls    :    none
  1270. *  Called by    :    readcpic()
  1271. **********************************************************************/
  1272. long unrleit(buf,bufto,outlen)
  1273. long outlen;    /* the long value of the final unencoded line length */
  1274. unsigned char *buf,*bufto;    /* the character pointers to the initial and final character buffers */
  1275. {
  1276.     register int cnt;
  1277.     register unsigned char *p,*q;
  1278.     unsigned char *endp;
  1279.     static unsigned char save[255], *savestart=NULL, *saveend=NULL;
  1280.                 /* save has a list of decompressed bytes not returned in
  1281.                     previous call.  savestart and saveend specify the position
  1282.                     at which this list starts and ends in the array save */
  1283.     
  1284.     p=buf;
  1285.     endp=bufto+outlen;
  1286.     q=bufto;
  1287.     while((saveend>savestart) && (q<endp))            /* copy saved stuff */
  1288.         *q++=*savestart++;
  1289.     if(savestart>=saveend)
  1290.         savestart=saveend=save;        /* all copied */
  1291.     while(q<endp) {
  1292.         cnt=*p++;            /* count field */
  1293.         if(!(cnt&128))    /* is set of uniques */
  1294.             while(cnt--)
  1295.                 if(q<endp)
  1296.                     *q++=*p++;    /* copy unmodified */
  1297.                 else
  1298.                     *saveend++=*p++;
  1299.         else {
  1300.             cnt&=127;            /* strip high bit */
  1301.             while(cnt--)
  1302.                 if(q<endp)
  1303.                     *q++=*p;    /* copy unmodified */
  1304.                 else
  1305.                     *saveend++=*p;
  1306.             p++;                /* skip that character */
  1307.           }    /* end else */
  1308.       }    /* end while */
  1309.     return((long)(p-buf));
  1310. }    /* end unrleit() */
  1311.  
  1312. /************************************************************************
  1313. *  Function    : unimcomp
  1314. *  Purpose    : 'Decompresses' the compressed image
  1315. *  Parameter    :
  1316. *    xdim       - x dimensions of image
  1317. *    lines      - number of lines of compressed image
  1318. *    in, out    - Input buffer and output buffer. Size of input buffer
  1319. *           is xdim*lines. Size of output buffer is 4 times
  1320. *           that. It 'restores' images into seq-type files
  1321. *  Returns      : none
  1322. *  Called by   : readipic(), read_pic()
  1323. *  Calls       : none
  1324. ************************************************************************/
  1325. void unimcomp(xdim,lines,in,out)
  1326. int xdim,    /* width of the image */
  1327.     lines;    /* number of lines of the compressed image */
  1328. unsigned char in[], /* input buffer */
  1329.             out[];    /* output buffer (4 times as large as the input buffer) */
  1330. {
  1331.   int bitmap,    /* integer value of two side-by-side pixels */
  1332.         temp;    /* temporary var. to hold working stuff */
  1333.   int i, j, k,    /* local counting variables */
  1334.         x, y;    /* counting variables for the x & y coor. of the image */
  1335.   unsigned char hi_color,    /* value of the highest color */
  1336.                 lo_color;    /* value of the lowest color */
  1337.  
  1338.   /* go over the compressed image */
  1339.   for (y=0; y<lines; y++)
  1340.     for (x=0; x<xdim; x=x+4) {
  1341.       k=y*xdim + x;
  1342.       hi_color=in[k+2]; 
  1343.       lo_color=in[k+3];
  1344.  
  1345.       bitmap=(in[k] << 8) | in[k+1];
  1346.  
  1347.       /* store in the out buffer */
  1348.       for (i=(y*4); i<(y*4+4); i++) {
  1349.         temp=bitmap >> (3 + y*4 - i)*4;
  1350.         for (j=x; j<(x+4); j++) {
  1351.            if ((temp & 8) == 8)
  1352.             out[i*xdim+j]=hi_color;
  1353.           else
  1354.             out[i*xdim+j]=lo_color;
  1355.           temp=temp << 1;
  1356.         } /* end of for j */
  1357.       }    /* end of for i */
  1358.     } /* end of for x */
  1359. } /* end of unimcomp() */
  1360.  
  1361. /************************************************************************
  1362. *  Function   : select_pal
  1363. *  Purpose    : Selects a palette of 16 colors out of 256 colors.
  1364. *               Each of the 256 original colors are 24 bit-rgb values
  1365. *               The routine truncates the 24 bit values to 6 bits and
  1366. *               selects the most popular 16 colors
  1367. *  Parameters :
  1368. *    xdim,ydim - x and y dimension of image
  1369. *    mode      - '1' implies program will count the frequency of colors
  1370. *                '0' implies the frequency count has alreay been done
  1371. *  Returns    : none
  1372. *  Called By  : showega()
  1373. *  Calls      : choose_rgb(), map()
  1374. ************************************************************************/
  1375. void select_pal(xdim,ydim,mode)
  1376. int xdim,ydim,    /* x & y dimensions */
  1377.         mode;    /* whether or not to perform frequency count */
  1378. {
  1379.     int i,j,k,    /* local counting variables */
  1380.         x,y,    /* variables for x & y dimensions */
  1381.         indx;    /* index to the current maximum value in the frequency table */
  1382.     long maxfreq;    /* the current highest frequency */
  1383.     unsigned char color;    /* the color of a pixel */
  1384.  
  1385.     int choose_rgb();    /* routine to choose a corresponding color from a mixture of red, green, and blue palettes */
  1386.     int map();    /* routine to map from 16 colors into the 256 color vga table */
  1387.  
  1388.     /* count the frequency of each color used */
  1389.  
  1390.     textmode();
  1391.     printf("Performing Frequency Count on The Image Data, Please be Patient\n");
  1392.     if (mode == 1) {    /* if mode!=1 then count has already been performed, so no need to do it again */
  1393.          for (i=0; i<64; i++)    /* clear out the count array */
  1394.             count[i]=0;
  1395.           for (y=0; y<ydim; y++)
  1396.             for (x=0; x<xdim; x++) {
  1397.                 color=store[y][x];
  1398.                 j=3 * color;
  1399.                 k=choose_rgb(pal[j],pal[j+1],pal[j+2]);
  1400.                 count[k]++;
  1401.              }    /* end of for x */
  1402.       }    /* end of if mode==1 */
  1403.  
  1404. /* select the 16 most frequently used colors */
  1405.  
  1406.     for (i=0; i<16; i++) {
  1407.         maxfreq=count[0];
  1408.         indx=0;
  1409.         for (j=1; j<64; j++)
  1410.             if (maxfreq < count[j]) {
  1411.                 maxfreq=count[j];
  1412.                 indx=j;
  1413.               }    /* end of for j */
  1414.         count[indx]=0;
  1415.         regrs[i]=indx;
  1416.         egapal(i,indx);
  1417.       }    /* end of for i */
  1418.  
  1419. /*  set up translation table */
  1420.  
  1421.     for (i=0; i<768; i=i+3) {
  1422.         j=choose_rgb(pal[i],pal[i+1],pal[i+2]);
  1423.         trans[i/3]=map(regrs,j);
  1424.       }    /* end of for i */
  1425.     grafmode();
  1426. } /* end of select_pal() */
  1427.  
  1428. /************************************************************************
  1429. *  Function   : map
  1430. *  Purpose    : Maps a 6 bit rgb value to one of the 16 colors chosen
  1431. *               Selects the one with the least bit difference
  1432. *  Parameters :
  1433. *    regrs     - stores the 16 colors chosen
  1434. *    color     - the color to be mapped
  1435. *  Returns    : register with nearest color
  1436. *  Called By  : select_pal()
  1437. *  Calls      : none
  1438. ************************************************************************/
  1439. int map(regrs,color)
  1440. char regrs[16];    /* array of the current ega palette values */
  1441. int color;    /* the color to map into the large 256 color palette */
  1442. {
  1443.     int i,j,    /* counting variables */
  1444.         minval,    /* the smallest value in the 256 color palette which correspond to the color chosen */
  1445.         temp,    /* temporary variable */
  1446.         diff,    /* the amount of difference between a color and the ega palette value */
  1447.         reg;    /* the register number of the ega palette which is closest to the color wanted */
  1448.  
  1449.     minval=999;
  1450.     for (i=0; i<16; i++) {    /* check through all the registers in the ega palette */
  1451.         diff=0;
  1452.         temp=1;
  1453.         for (j=0; j<6; j++) {    /* check through all the bits in the color */
  1454.             if ((temp & color) != (regrs[i] & temp))    /* if the bits don't match then increment the difference variable */
  1455.                 diff++;
  1456.             temp=temp * 2;
  1457.           }    /* end for j */
  1458.         if (minval > diff) {
  1459.             reg=i;
  1460.             minval=diff;
  1461.           }    /* end if */
  1462.       }    /* end for i */
  1463.     return reg;    /* return the corresponding register value to the color given */
  1464. } /* end of map() */
  1465.  
  1466. /************************************************************************
  1467. *  Function   : choose_rgb
  1468. *  Purpose    : Selects a 6 bit rgb value for a 24 bit number. Takes
  1469. *               the 2 most sign. bit of each 8 bit component
  1470. *  Parameters :
  1471. *    r,g,b    - rgb components
  1472. *  Returns    : 6 bit rgb value
  1473. *  Called By  : select_pal()
  1474. *  Calls      : none
  1475. ************************************************************************/
  1476. int choose_rgb(r,g,b)
  1477. int r,g,b;    /* red, green, and blue components for the 24 bit value */
  1478. {
  1479.     int color;    /* 6 bit color value */
  1480.  
  1481.     color=((r & 128) >> 5) | ((r & 64) >> 1) |
  1482.             ((g & 128) >> 6) | ((g & 64) >> 2) |
  1483.             ((b & 128) >> 7) | ((b & 64) >> 3);
  1484.  
  1485.     return color;    /* return the 6 bit color value */
  1486. } /* end choose_rgb() */
  1487.  
  1488. /**********************************************************************
  1489. *  Function    :    newpal
  1490. *  Purpose    :    read a new palette from a binary file and switch to it
  1491. *  Parameters    :
  1492. *            s - name of a file
  1493. *  Returns    :    none
  1494. *  Calls    :    palloc(), palloce(), select_pal()
  1495. *  Called by    :    main(), options()
  1496. **********************************************************************/
  1497. void newpal(s)
  1498. char *s;    /* pointer to a file name */
  1499. {
  1500.     FILE *fp;    /* file pointer for the palette */
  1501.     
  1502.     if (NULL == (fp=fopen(s,"rb")))    /* open the palette file */
  1503.         makepal();
  1504.     else {    /* read in the palettes */
  1505.         fread(rmap,1,256,fp);
  1506.         fread(gmap,1,256,fp);
  1507.         fread(bmap,1,256,fp);
  1508.         fclose(fp);
  1509.  
  1510. /* attempt to allocate room for the palette and switch to it */
  1511.  
  1512.         palmax++;
  1513.         if(mode!=EGA) {
  1514.             if(!palloc(palmax)) {    /*allocate the new palette memory */
  1515.                 palmax--;            /* bad allocation, fix old palette */
  1516.                 displayerr("No Room For Another Palette");
  1517.                 memcpy(rmap,rpal[palnum],256);
  1518.                 memcpy(gmap,gpal[palnum],256);
  1519.                 memcpy(bmap,bpal[palnum],256);
  1520.               } /* end if */
  1521.             else 
  1522.                 palnum=palmax;    /* good allocation, set palnum to new palette */
  1523.           } /* end if */
  1524.         else {
  1525.             for(i=0; i<256; i++) {    /* put individual palettes into the big one */
  1526.                 pal[i*3]=rmap[i];
  1527.                 pal[i*3+1]=gmap[i];
  1528.                 pal[i*3+2]=bmap[i];
  1529.               } /* end for */
  1530.             select_pal(xdim,ydim,1);    /* do a frequency count on it */
  1531.             if(!palloce(palmax)) {
  1532.                 displayerr("No Room For Another Palette");
  1533.                 palmax--;
  1534.                 memcpy(regrs,egapals[palnum],16);
  1535.               } /* end if */
  1536.             else 
  1537.                 palnum=palmax;
  1538.           } /* end else */
  1539.       } /* end else */    
  1540. } /* end newpal() */
  1541.  
  1542. /**********************************************************************
  1543. *  Function    :    textmode
  1544. *  Purpose    :    switch into text mode
  1545. *  Parameters    :    none
  1546. *  Returns    :    none
  1547. *  Calls    :    resetega(), vgamode(3)
  1548. *  Called by    :    main(), options()
  1549. **********************************************************************/
  1550. void textmode()
  1551. {
  1552.     switch(mode){    /* depending on the monitor type, switch to text display mode */
  1553.         case VGA:
  1554.             vgamode(3);
  1555.             break;
  1556.  
  1557.         case NO9:
  1558.             vgamode(3);
  1559.             break;
  1560.  
  1561.         case EGA:
  1562.         default:
  1563.             resetega();
  1564.             break;
  1565.       } /* end switch */
  1566. } /* end textmode() */
  1567.  
  1568. /**********************************************************************
  1569. *  Function    :    grafmode
  1570. *  Purpose    :    switch into correct graphics mode
  1571. *  Parameters    :    none
  1572. *  Returns    :    none
  1573. *  Calls    :    vgamode(), initega(), putpal()
  1574. *  Called by    :    main(), options()
  1575. **********************************************************************/
  1576. void grafmode()
  1577. {
  1578.     switch (mode){    /* depending on the monitor type, switch to graphics display mode */
  1579.         case VGA:
  1580.             vgamode(19);
  1581.             break;
  1582. /*
  1583.         case TARGA:
  1584.             GraphInit(16);
  1585.             break;
  1586. */
  1587.         case NO9:
  1588.             RG9clrscr();
  1589.             break;
  1590.  
  1591.         case EGA:
  1592.         default:
  1593.             initega();
  1594.             break;
  1595.       }    /* end switch */
  1596.     putpal();    /* replace the palette */
  1597. } /* end grafmode() */
  1598.  
  1599. /**********************************************************************
  1600. *  Function    :    putpal
  1601. *  Purpose    :    renew the palette on the graphics card with the current
  1602. *            palette in memory
  1603. *  Parameters    :    none
  1604. *  Returns    :    none
  1605. *  Calls    :    putmapv(), putmap9(), egapal(),
  1606. *  Called by    :    grafmode(), options()
  1607. **********************************************************************/
  1608. void putpal()
  1609. {
  1610.     switch (mode){        /* different calls for different monitor types */
  1611.         case VGA:
  1612.             putmapv(rmap,gmap,bmap);
  1613.             break;
  1614.  
  1615.         case NO9:
  1616.             putmap9(rmap,gmap,bmap);
  1617.             break;
  1618.  
  1619.         case EGA:
  1620.         default:
  1621.             for(i=0; i<16; i++)
  1622.                 egapal(i,(int)regrs[i]);
  1623.             break;
  1624.       } /* end switch */
  1625. } /* end putpal() */
  1626.  
  1627. /**********************************************************************
  1628. *  Function    :    showpic
  1629. *  Purpose    :    display the current portion of the image
  1630. *  Parameters    :    none
  1631. *  Returns    :    none
  1632. *  Calls    :    vgaline(), egaline(), no9line()
  1633. *  Called by    :    main(), options()
  1634. **********************************************************************/
  1635. void showpic()
  1636. {
  1637.     switch(mode){        /* output to screen differently for each different type of monitor */
  1638.         case VGA:
  1639.             for(i=0; (i+ywhere)<ydim && (i+ywhere)<200; i++)
  1640.                 LINEVGA;
  1641.             break;
  1642.  
  1643.         case NO9:
  1644.             for(i=0; (i+ywhere)<ydim && (i+ywhere)<500; i++)
  1645.                 LINENO9;
  1646.             break;
  1647. /*
  1648.          case TARGA:
  1649.              for(i=0; (i+ywhere)<ydim && (i+ywhere)<500; i++)
  1650.                  LINET16;
  1651.              break;            
  1652. */
  1653.         case EGA:
  1654.         default:
  1655.             for (i=0; (i+ywhere)<ydim && (i+ywhere)<350; i++)           /* display the image */
  1656.                 LINEEGA;
  1657.             break;
  1658.       }    /* end switch */
  1659. } /* end showpic() */
  1660.  
  1661. /**********************************************************************
  1662. *  Function    :    initpal
  1663. *  Purpose    :    to load in the first palette for the image
  1664. *  Parameters    :    none
  1665. **  Returns    :    none
  1666. *  Calls    :    loadpal(), loadega()
  1667. *  Called by    :    main()
  1668. **********************************************************************/
  1669. void initpal()
  1670. {
  1671.         switch(mode){        /* VGA and No9 use 256 byte palettes and EGA uses 16 byte palettes */
  1672.             case VGA:
  1673.             case NO9:
  1674.                 loadpal();
  1675.                 break;
  1676.  
  1677.              case TARGA:
  1678.                  loadpal();
  1679.                  break;
  1680.  
  1681.             case EGA:
  1682.             default:
  1683.                 loadega();
  1684.                 break;
  1685.           }    /* end switch(mode) */
  1686. } /* end initpal() */
  1687.  
  1688. /**********************************************************************
  1689. *  Function    :    makepal
  1690. *  Purpose    :    make a grey scale palette as the first palette
  1691. *  Parameters    :    none
  1692. *  Returns    :    none
  1693. *  Calls    :    select_pal(), palloc(), palloce(), putpal9(), putpalv(),
  1694. *                memcpy()
  1695. *  Called by    :    newpal()
  1696. **********************************************************************/
  1697. void makepal()
  1698. {
  1699.     if(mode!=EGA) {        /* for vga and no9 palettes */
  1700.         for (i=0; i<256; i++)        /* make a grey scale palette */
  1701.             rmap[i]=gmap[i]=bmap[i]=i;
  1702.  
  1703.         palnum=0;
  1704.         palmax=0;
  1705.         if(!palloc(palmax)) {    /* allocate room for another palette */
  1706.             palmax=-1;
  1707.             displayerr("No Room For Another Palette");
  1708.           }    /* end if */
  1709.         else                /* load the grey scale data into the arrays */
  1710.             switch (mode) {        /* switch routine to put the correct palette */
  1711.                 case VGA:
  1712.                     putmapv(&rmap[0],&gmap[0],&bmap[0]);
  1713.                     break;
  1714.  
  1715.                 case NO9:
  1716.                     putmap9(&rmap[0],&gmap[0],&bmap[0]);
  1717.                     break;
  1718.               }    /* end switch */
  1719.       }    /* end if */
  1720.     else {        /* for ega palettes */
  1721.         for(i=0; i<256; i++)    /* make an ega grey scale big palette */
  1722.             pal[i*3]=pal[i*3+1]=pal[i*3+2]=i;
  1723.         palnum=0;
  1724.         palmax=0;
  1725.         select_pal(xdim,ydim,1);    /* perform a frequency count on it */
  1726.         if(!palloce(palmax)) {    /* allocate room for the first palette */
  1727.             palmax=-1;
  1728.             displayerr("No Room For Another Palette");
  1729.           }    /* end if */
  1730.         else 
  1731.             for(i=0; i<16; i++)
  1732.                 egapal(i,(int)regrs[i]);
  1733.       }    /* end else */
  1734.     if(palmax==-1)            /* grey scale not loaded properly */
  1735.         displayerr("Palette Not Specified, No Room For Any Palettes");
  1736.     else                        /* successful grey scale load */        
  1737.         displayerr("Palette Not Specified, Grey Scale Palette Loaded");
  1738. }    /* end makepal() */
  1739.  
  1740. /**********************************************************************
  1741. *  Function    :    displayerr
  1742. *  Purpose    :    display a string in textmode, wait for keypress and
  1743. *                to graphics mode
  1744. *  Parameters    :
  1745. *            errstr -    the string to be displayed on the screen
  1746. *  Returns    :    none
  1747. *  Calls    :    textmode, updatescrn()
  1748. *  Called by    :    everywhere...
  1749. **********************************************************************/
  1750. void displayerr(errstr)
  1751. char *errstr;
  1752. {
  1753.     if(mouse && mode==NO9)
  1754.         makecur9(mx,my);
  1755.     textmode();
  1756.     printf("%s\n",errstr);
  1757.     printf("Hit any key to continue\n");
  1758.     waitq();
  1759.     grafmode();
  1760.     updatescrn();
  1761. }    /* end displayerr() */
  1762.  
  1763. /**********************************************************************
  1764. *  Function    :    debug
  1765. *  Purpose    :    print debugging variables
  1766. *  Parameters    :
  1767. *            s1,s2,s3,s4 -    names of variables printed
  1768. *            d1,d2,d3,d4 -    variables to print
  1769. *  Returns    :    none
  1770. *  Calls    :    none
  1771. *  Called by    :    anywhere...
  1772. **********************************************************************/
  1773. void debug(s1,d1,s2,d2,s3,d3,s4,d4)
  1774. char *s1,*s2,*s3,*s4;
  1775. int    d1,d2,d3,d4;        /* variables to display */
  1776. {
  1777.     if(mouse && mode==NO9)
  1778.         makecur9(mx,my);
  1779.     textmode();
  1780.     printf("%s=%d\n",s1,d1);
  1781.     printf("%s=%d\n",s2,d2);
  1782.     printf("%s=%d\n",s3,d3);
  1783.     printf("%s=%d\n",s4,d4);
  1784.     printf("Hit any key to continue\n");
  1785.     waitq();
  1786.     grafmode();
  1787.     updatescrn();
  1788. }    /* end debug() */
  1789.  
  1790. /**********************************************************************
  1791. *  Function    :    usage
  1792. *  Purpose    :    print the command line parameters
  1793. *  Parameters    :    none
  1794. *  Returns    :    none
  1795. *  Calls    :    none
  1796. *  Called by    :    main()
  1797. **********************************************************************/
  1798. void usage()
  1799. {
  1800.     printf("Command line options are as follows:\n");
  1801.     printf("\t-b xdim ydim\t: The dimensions of the binary file, and specifies\n");
  1802.     printf("\t\t\tbinary file type of data\n");
  1803.     printf("\t-m mapfile\t: The name of a palette for a binary file\n");
  1804.     printf("\t-v\t: Sets VGA display mode\n");
  1805.     printf("\t-e\t: Sets EGA display mode (default)\n");
  1806.     printf("\t-9\t: Sets Number 9 display mode\n");
  1807.     printf("\t-h\t: Specifies reading from an HDF file (default)\n");
  1808.     printf("\t-c\t: Center the image on the screen\n");
  1809.     printf("\t-p xwhere ywhere  : Specify an x & y position for the upper left hand\n");
  1810.     printf("\t\t\tcorner of the screen\n");
  1811.     printf("\t-a\t: Set animation mode\n");
  1812.     printf("\t-A\t: Set animation mode and specify animation of multiple\n");
  1813.     printf("\t\t\timages from only one hdf file\n");
  1814.     printf("\t-s\t: Specifies only one binary image and name is filename\n");
  1815.     printf("\tfilename\t: The file containing a list of binary images or\n");
  1816.     printf("\t\t\tan HDF file containing images\n\n");
  1817.     printf("Note:  for binary file usage, both the -b xdim ydim and the -m mapfile\n");
  1818.     printf("\toptions must be used for the correct displaying of the image.\n");
  1819.     printf("\tThe file name at the end of the command line is a mandatory command\n");
  1820.     printf("\tline parameter, the rest of the command line parameters are optional.\n");
  1821. }
  1822.  
  1823. /**********************************************************************
  1824. *  Function    :    t16line
  1825. *  Purpose    :    Convert Image to Targa format and display
  1826. *  Parameters    :
  1827. *            x,y    -    x & y coor. for the position of the line
  1828. *            store -    the pointer for the image
  1829. *            xoff -     offset into the line
  1830. *            len -    length of a line of the image
  1831. *  Returns    :    none
  1832. *  Calls    :    pline()
  1833. *  Called by    :    showpic()
  1834. **********************************************************************/
  1835. /*void t16line(x,y,store,xoff,len)
  1836. /*int x,y,xoff,len;
  1837. /*unsigned char *store;
  1838. /*{
  1839. /*    short int i,        /* counter variable */
  1840. /*            v,            /* variable to keep track of the color for the targa */
  1841. /*            ind;        /* data from the image line */
  1842. /*     unsigned short int *ip;    /* pointer to the current targa image line */
  1843.  
  1844. /*     ip=l16;
  1845.  
  1846. /*    for (i=0; i<len; i++) {
  1847. /*        ind=*(store+i);        /* get the data from image in memory */
  1848. /*        v=bmap[ind]>>3;        /* convert into targa form */
  1849. /*        v |= (gmap[ind]>>3)<<5;
  1850. /*         v |= (rmap[ind]>>3)<<10;
  1851. /*         *ip++=v;                /* store in targa line */
  1852. /*      }    /* end for */
  1853. /*     pline(x,485-y,l16,len);        /* call targa line routine */
  1854. /* }*/
  1855.  
  1856. void NO9setup()
  1857. /* prepare variables for use in other functions */
  1858. {
  1859.     NO9ram=(char *)0xA0000;
  1860.     NO9bank=(int *)0xC0705;
  1861. }
  1862.  
  1863.  
  1864. void clrbuf(bank)
  1865. unsigned int bank;
  1866. {
  1867.     *NO9bank=bank;
  1868.     memset(NO9ram,0,0xFFFF);
  1869.     NO9ram[0xFFFF]=0;
  1870. }
  1871.  
  1872.  
  1873. void RG9gmode()
  1874. /* go into NO9 graphics mode -- not yet implemented */
  1875. {
  1876.     clrbuf(0x0000);
  1877.     clrbuf(0x00ff);
  1878.     clrbuf(0xff00);
  1879.     clrbuf(0xffff);
  1880. }
  1881.  
  1882. void RG9clrscr()
  1883. /* 
  1884.     Clear the screen.
  1885. */
  1886. {
  1887.         NO9setup();
  1888.         RG9gmode();
  1889. }
  1890.